package com.common.app.widget.calendardialog

import android.app.Dialog
import android.content.Context
import android.view.View
import com.common.app.R
import com.common.base.R as BaseR

/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：30/5/2025
 *
 * 描述：
 *
 * 修订历史：
 *
 */
class CusCalendarViewDialog private constructor() {
    private var dialog: Dialog? = null
    private val marksDays: MutableList<Long> = ArrayList()
    private var calendarView: CusCalendarView? = null
    private var context: Context? = null

    /**
     * 显示
     *
     * @param context
     * @return
     */
    fun init(context: Context): CusCalendarViewDialog {
        if (dialog == null || calendarView == null || this.context !== context) {
            this.context = context
            dialog = Dialog(context, BaseR.style.VerticalDialog)
            val view = View.inflate(context, R.layout.cus_dialog_calendar, null)
//            view.setOnTouchListener { v, event ->
//                //Log.e("hdltag", "onTouch(CalendarViewDialog.java:53):--------------------");
//                true
//            }
            calendarView = view.findViewById(R.id.robotoCalendarPicker)
            calendarView?.setShortWeekDays(true)
            calendarView?.showDateTitle(true)
            calendarView?.updateView()
            dialog!!.setContentView(view)
            val dm = context.resources.displayMetrics
            val displayWidth = dm.widthPixels
            val displayHeight = dm.heightPixels
            val p = dialog!!.window!!.attributes //获取对话框当前的参数值
            p.width = (displayWidth * 0.9).toInt() //宽度设置为屏幕的0.55
            p.height = (displayHeight * 0.54).toInt() //高度设置为屏幕的0.28
            dialog!!.window!!.attributes = p //设置生效
        }
        return this
    }

    private var onCalendarClickListener: CusCalendarView.OnCalendarClickListener? = null
    fun show(onCalendarClickListener: CusCalendarView.OnCalendarClickListener?) {
        this.onCalendarClickListener = onCalendarClickListener
        if (onCalendarClickListener != null) {
            calendarView?.setOnCalendarClickListener(onCalendarClickListener)
        }
        if (dialog != null) {
            dialog!!.show()
        }
        if (currentTimeMillis != 0L) {
//            Log.e("hdltag", "show(CalendarViewDialog.java:86):----------------"+DateUtils.getDateByCurrentTime(calendarView.getCurrentSelectedDay().getTimeInMillis()));
            calendarView?.setSelectedDay(calendarView?.getCurrentSelectedDay()?.timeInMillis ?:0)
        }
    }

    fun addMarks(marks: List<Long>): CusCalendarViewDialog {
        marksDays.clear()
        marksDays.addAll(marks)
        calendarView?.addMarkDays(marksDays)
        return this
    }

    fun close() {
        if (dialog != null) {
            dialog!!.dismiss()
        }
    }

    var isLimitMonth = false
        private set

    fun setLimitMonth(limitMonth: Boolean): CusCalendarViewDialog {
        isLimitMonth = limitMonth
        calendarView?.setLimitMonth(isLimitMonth)
        return this
    }

    private var currentTimeMillis: Long = 0

    fun setSelectedDay(currentTimeMillis: Long): CusCalendarViewDialog {
        this.currentTimeMillis = currentTimeMillis
        calendarView?.setSelectedDay(currentTimeMillis)
        return this
    }

    companion object {
        private var mCalendarViewDialog: CusCalendarViewDialog? = null
        val instance: CusCalendarViewDialog?
            get() {
                if (mCalendarViewDialog == null) {
                    synchronized(CusCalendarViewDialog::class.java) {
                        if (mCalendarViewDialog == null) {
                            mCalendarViewDialog = CusCalendarViewDialog()
                        }
                    }
                }
                return mCalendarViewDialog
            }
    }
}